package com.yuncheng.oaApi.busService;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.entity.OaSw;
import com.yuncheng.spcyApi.entity.SpcyRsJjrk;
import com.yuncheng.oaApi.service.IOaSwService;
import com.yuncheng.oaApi.service.IOaSpcyRsJjrkService;
import groovy.util.logging.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import utils.WorkDateUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
@EnableScheduling
@EnableAsync
@Slf4j
public class UpdateSyts {
    private static final Logger log = LoggerFactory.getLogger(UpdateSyts.class);

    @Resource
    @Lazy
    private IOaSwService oaSwService;

    @Resource
    @Lazy
    private IOaSpcyRsJjrkService oaSpcyRsJjrkService;


    @Configuration
    @EnableScheduling
    public class TaskSchedulerConfig {
        @Bean
        public TaskScheduler taskScheduler2() {
            ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
            scheduler.setPoolSize(10);
            scheduler.initialize();
            return scheduler;
        }
    }


    @Scheduled(cron = "0 0 0 * * ?")  //每天的凌晨执行
    public void updatesyts() {
        Date startdate = new Date();

        int ts = 0;
        List<OaSw> tslist = oaSwService.list(new LambdaQueryWrapper<OaSw>().gt(OaSw::getFzsyts, ts));
        Date nowdate = new Date();
        //查询当天是否调休 或者加班
        // 调休数据
        List<Date> txDateList = new ArrayList<>();
        List<SpcyRsJjrk> txList = oaSpcyRsJjrkService.list(new LambdaQueryWrapper<SpcyRsJjrk>()
                .like(SpcyRsJjrk::getFjjrlx, OaCommonFlowConstant.JJRK_LX_TX));
        if (CollectionUtils.isNotEmpty(txList)) {
            txDateList = txList.stream().map(SpcyRsJjrk::getFjjrsj).collect(Collectors.toList());
        }
        boolean tx = WorkDateUtils.isExists(nowdate, txDateList); //当前时间是否调休


        //加班数据
        List<Date> jbDateList = new ArrayList<>();
        List<SpcyRsJjrk> jbList = oaSpcyRsJjrkService.list(new LambdaQueryWrapper<SpcyRsJjrk>()
                .like(SpcyRsJjrk::getFjjrlx, OaCommonFlowConstant.JJRK_LX_JB));
        if (CollectionUtils.isNotEmpty(jbList)) {
            jbDateList = jbList.stream().map(SpcyRsJjrk::getFjjrsj).collect(Collectors.toList());
        }
        boolean jb = WorkDateUtils.isExists(nowdate, jbDateList); //当前时间是否加班


        Calendar cal = Calendar.getInstance();
        cal.setTime(nowdate);
        int week = cal.get(Calendar.DAY_OF_WEEK) - 1; //获取星期几
        log.info("更新数据为" + tslist);


        //先判断当前时间是否是加班 如果是加班直接减一
        if (!jb) { //如果不是加班 在判断是不是周六周日 和调休
            if (!(week == 6 || week == 0 || tx)) { //0为周日，6为周六 如果不是周六周日 就减去一 ||是周六周日 h调休
                for (OaSw swts : tslist) {
                    Integer fzsyts = swts.getFzsyts();
                    fzsyts = fzsyts - 1;
                    swts.setFzsyts(fzsyts);
                    oaSwService.updateById(swts);
                }
            }
        } else {
            for (OaSw swts : tslist) {
                Integer fzsyts = swts.getFzsyts();
                fzsyts = fzsyts - 1;
                swts.setFzsyts(fzsyts);
                oaSwService.updateById(swts);
            }
        }
        Date enddate = new Date();
        long ms = enddate.getTime() - startdate.getTime();

       log.info("更新时间为" + ms + "ms");
    }
}
